package other;

/**
 * @Author wangtengyu
 * @Create 2018-09-11-15:37 求数值的整数次方 exponent 指数 该题的误区，要考虑 base 和exponent 如果exponent为0 为负数
 */
public class Power {


  public double pow(double base, int exponent) {

    if (base == 0) {
      return 0.0;
    }
    if (exponent == 0) {
      return 1;
    }

    double result = 1.0;
    if (exponent > 0) {
      while (exponent > 0) {
        result = result * base;
        exponent--;
      }
    }

    if (exponent < 0) {
      exponent = Math.abs(exponent);
      while (exponent > 0) {
        result = base * result;
        exponent--;
      }
      result = 1 / result;
    }

    return result;
  }

  public static void main(String[] args) {
    Power power = new Power();
    double result = power.pow(-3, -2);
    System.out.println(result);
  }


}
